﻿using Biotech.Log;
using project.B12.Model.BaseModel;
using project.B12.Model.Enums;
using SqlSugar;

namespace project.B12.Model.Models
{
    [SugarTable("TB_Running")]
    [TenantAttribute("1")]
    public class Table_MonitorMes : AggregateRootEntity
    {     
        public System.Threading.Timer? mytimer;

        public Table_MonitorMes()
        {
            SpecialSampleTypes = new Dictionary<string, List<int>>();
            PhotoCount = 0;
            PhotoIntervalTime = -1;
            IsReport = false;
        }

        #region 属性
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id {  get; set; }

        private HoleState _state;
        [SugarColumn(IsNullable = true)]
        public HoleState State
        {
            get { return _state; }
            set 
            { 
                _state = value; 
                this.RaisePropertyChanged(); 
                if(_state==HoleState.Incubation)
                {
                    mytimer = new Timer(new TimerCallback(IncubationTiming), null, 0, 1000);
                    mytimer.Change(0, 1000);
                }
                this.RaisePropertyChanged();
            }
        }

        [SugarColumn(IsNullable = true)]
        public int Shelf { get; set; }

        [SugarColumn(IsNullable = true)]
        public int Index { get; set; }

        private string _plateId;
        [SugarColumn(IsNullable = true)]
        public string PlateID
        {
            get { return _plateId; }
            set
            {
                _plateId = value;
                this.RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 药敏板项目
        /// </summary>
        private string _plateType;
        [SugarColumn(IsNullable =true)]
        public string PlateType
        {
            get { return _plateType; }
            set
            {
                _plateType = value;
                this.RaisePropertyChanged();
            }
        }

        [SugarColumn(IsNullable =true)]
        public string SampleID { get; set; }

        /// <summary>
        /// 菌名称
        /// </summary>
        [SugarColumn(IsNullable =true)]
        public string SampleType { get; set; }

        /// <summary>
        /// 是否特殊加样
        /// </summary>
        [SugarColumn(IsNullable = false)]
        public bool IsSpecialSample { get; set; }

        /// <summary>
        /// 特殊加样的菌名称
        /// </summary>
        [SugarColumn(IsNullable = true, IsJson = true)]
        public Dictionary<string,List<int>> SpecialSampleTypes { get; set; }

        /// <summary>
        /// 特殊加样,是否按列加样
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public bool IsColumn { get; set; }

        [SugarColumn(IsNullable =true)]
        public DateTime? LoadTime { get; set; }

        [SugarColumn(IsNullable =true)]
        public DateTime? AddSampleTime { get; set; }

        [SugarColumn(IsNullable =true)]
        public DateTime? PhotoTime { get; set; }

        /// <summary>
        /// 左右加样位，0表示左加样
        /// </summary>
        private int _lorR;
        [SugarColumn(IsNullable = true)]
        public int LorR
        {
            get { return _lorR; }
            set
            {
                _lorR = value;
                this.RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 培育时间
        /// </summary>
        private int _cultureTime;
        [SugarColumn (IsNullable =true)]
        public int CultureTime 
        { 
            get { return _cultureTime; }
            set
            {
                _cultureTime = value;
                this.RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 孵育倒计时
        /// </summary>
        private string _countdown;
        [SugarColumn(IsIgnore =true)]
        public string Countdown
        {
            get { return _countdown; }
            set
            {
                _countdown = value;
                this.RaisePropertyChanged();
                //IncubationTiming(_countdown);
            }
        }

        private bool _isChecked;
        [SugarColumn(IsIgnore = true)]
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                this.RaisePropertyChanged();
                //IncubationTiming(_countdown);
            }
        }

        /// <summary>
        /// 是否发生过关机
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public bool ShutdownOrNot {  get; set; }

        /// <summary>
        /// 是否需要去拍照
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public bool IsPhoto {  get; set; }

        /// <summary>
        /// 间隔拍照的时间
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public int PhotoIntervalTime { get; set; }

        /// <summary>
        /// 间隔拍照的次数
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public int PhotoCount { get; set; }

        /// <summary>
        /// 是否需要结果分析
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public bool IsReport {  get; set; }
        #endregion

        #region 方法

        /// <summary>
        /// 孵育倒计时
        /// </summary>
        /// <param name="a"></param>
        public void IncubationTiming(object a)
        {
            if (AddSampleTime != null)
            {
                if ((AddSampleTime + new TimeSpan(0, 0, _cultureTime)) > DateTime.Now)
                {
                    Countdown = (AddSampleTime + new TimeSpan(0, 0, _cultureTime) - DateTime.Now).Value.ToString(@"d\天hh\:mm\:ss");
                }
                else
                {
                    mytimer.Dispose();
                    State = HoleState.WaitingForPhotograph;
                    LoggerFactory.Instance.CurrentLogger.LogInfo("结束孵育", "", "软件");
                    Countdown = string.Empty;
                }
                if(!string.IsNullOrEmpty(Countdown))
                {
                    int time = PhotoIntervalTime * PhotoCount;
                    if (PhotoIntervalTime > 0 && (AddSampleTime + new TimeSpan(0, time, 0)) < DateTime.Now)
                    {
                        IsPhoto = true;
                        //PhotoCount++;
                    }
                }
            }
        }

        public void BindingSample(string sampleID, string bacteriumName)
        {
            SampleID = sampleID;
            SampleType = bacteriumName;
        }

        public void GoToAddSample()
        {
            State = HoleState.SAMPLING;
            LoggerFactory.Instance.CurrentLogger.LogInfo($"{PlateID} 开始加样", "", "软件");
        }
        public void AddSampleComplete()
        {
            AddSampleTime = DateTime.Now;
            State = HoleState.Incubation;
            LoggerFactory.Instance.CurrentLogger.LogInfo("开始孵育", "", "软件");
        }
        public void GoToPhoto()
        {
            State = HoleState.Photograph;
            LoggerFactory.Instance.CurrentLogger.LogInfo("开始拍照", "", "软件");
        }
        public void PhotoError()
        {
            PhotoTime = DateTime.Now;
            State = HoleState.PhotographError;
            LoggerFactory.Instance.CurrentLogger.LogInfo($"{PlateID}拍照错误", "", "软件");
        }
        public void PhotoComplete()
        {
            PhotoTime = DateTime.Now;
            State = HoleState.DONE;
            LoggerFactory.Instance.CurrentLogger.LogInfo("拍照完成", "", "软件");
        }
        #endregion

    }
}
